using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qABAUFM = from m in mtemp.ABAUFM select m;
       DBMapper.MapABAUFM(qABAUFM.ToList(), true);
       */
        public List<ABAUFM> MapABAUFM(XElement item2, bool mergedb)
        {
            List<ABAUFM> result = new List<ABAUFM>();
            var item4 = from item3 in item2.Elements()
                        select new ABAUFM
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            ABAMANDANT = (string)item3.Element("ABAMANDANT"),
                            ABAAUFTYP = (string)item3.Element("ABAAUFTYP"),
                            ABAAUFKRS = (string)item3.Element("ABAAUFKRS"),
                            ABAAUFLFD = (string)item3.Element("ABAAUFLFD"),
                            ABAAUFPOS = (string)item3.Element("ABAAUFPOS"),
                            ABAAPOS = (string)item3.Element("ABAAPOS"),
                            ABAVKAUFTYP = (string)item3.Element("ABAVKAUFTYP"),
                            ABAVKAUFKRS = (string)item3.Element("ABAVKAUFKRS"),
                            ABAVKAUFLFD = (string)item3.Element("ABAVKAUFLFD"),
                            ABAVKAUFPOS = (string)item3.Element("ABAVKAUFPOS"),
                            ABAVKUPOS = (string)item3.Element("ABAVKUPOS"),
                            ABAKUNDNR = (string)item3.Element("ABAKUNDNR"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapABAUFM(result, mergedb);
          return result;
        }
        ///DBMapper.MapABAUFM(mtemp.ABAUFM.ToList<ABAUFM>(), true);        
       public void MapABAUFM(List<ABAUFM> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from ABAUFM row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.ABAUFM.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.ABAUFM.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.ABAUFM.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "ABAUFM")]
    public class ABAUFM : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _ABAMANDANT="0";
        [Column]
        public string ABAMANDANT
        {
            get
            {
                return _ABAMANDANT;
            }
            set
            {
                if (value != _ABAMANDANT)
                {
                     _ABAMANDANT = value;
                    NotifyPropertyChanged("ABAMANDANT");
                }
            }
        }
        private string  _ABAAUFTYP="0";
        [Column]
        public string ABAAUFTYP
        {
            get
            {
                return _ABAAUFTYP;
            }
            set
            {
                if (value != _ABAAUFTYP)
                {
                     _ABAAUFTYP = value;
                    NotifyPropertyChanged("ABAAUFTYP");
                }
            }
        }
        private string  _ABAAUFKRS="0";
        [Column]
        public string ABAAUFKRS
        {
            get
            {
                return _ABAAUFKRS;
            }
            set
            {
                if (value != _ABAAUFKRS)
                {
                     _ABAAUFKRS = value;
                    NotifyPropertyChanged("ABAAUFKRS");
                }
            }
        }
        private string  _ABAAUFLFD="0";
        [Column]
        public string ABAAUFLFD
        {
            get
            {
                return _ABAAUFLFD;
            }
            set
            {
                if (value != _ABAAUFLFD)
                {
                     _ABAAUFLFD = value;
                    NotifyPropertyChanged("ABAAUFLFD");
                }
            }
        }
        private string  _ABAAUFPOS="0";
        [Column]
        public string ABAAUFPOS
        {
            get
            {
                return _ABAAUFPOS;
            }
            set
            {
                if (value != _ABAAUFPOS)
                {
                     _ABAAUFPOS = value;
                    NotifyPropertyChanged("ABAAUFPOS");
                }
            }
        }
        private string  _ABAAPOS="0";
        [Column]
        public string ABAAPOS
        {
            get
            {
                return _ABAAPOS;
            }
            set
            {
                if (value != _ABAAPOS)
                {
                     _ABAAPOS = value;
                    NotifyPropertyChanged("ABAAPOS");
                }
            }
        }
        private string  _ABAVKAUFTYP="0";
        [Column]
        public string ABAVKAUFTYP
        {
            get
            {
                return _ABAVKAUFTYP;
            }
            set
            {
                if (value != _ABAVKAUFTYP)
                {
                     _ABAVKAUFTYP = value;
                    NotifyPropertyChanged("ABAVKAUFTYP");
                }
            }
        }
        private string  _ABAVKAUFKRS="0";
        [Column]
        public string ABAVKAUFKRS
        {
            get
            {
                return _ABAVKAUFKRS;
            }
            set
            {
                if (value != _ABAVKAUFKRS)
                {
                     _ABAVKAUFKRS = value;
                    NotifyPropertyChanged("ABAVKAUFKRS");
                }
            }
        }
        private string  _ABAVKAUFLFD="0";
        [Column]
        public string ABAVKAUFLFD
        {
            get
            {
                return _ABAVKAUFLFD;
            }
            set
            {
                if (value != _ABAVKAUFLFD)
                {
                     _ABAVKAUFLFD = value;
                    NotifyPropertyChanged("ABAVKAUFLFD");
                }
            }
        }
        private string  _ABAVKAUFPOS="0";
        [Column]
        public string ABAVKAUFPOS
        {
            get
            {
                return _ABAVKAUFPOS;
            }
            set
            {
                if (value != _ABAVKAUFPOS)
                {
                     _ABAVKAUFPOS = value;
                    NotifyPropertyChanged("ABAVKAUFPOS");
                }
            }
        }
        private string  _ABAVKUPOS="0";
        [Column]
        public string ABAVKUPOS
        {
            get
            {
                return _ABAVKUPOS;
            }
            set
            {
                if (value != _ABAVKUPOS)
                {
                     _ABAVKUPOS = value;
                    NotifyPropertyChanged("ABAVKUPOS");
                }
            }
        }
        private string  _ABAKUNDNR="0";
        [Column]
        public string ABAKUNDNR
        {
            get
            {
                return _ABAKUNDNR;
            }
            set
            {
                if (value != _ABAKUNDNR)
                {
                     _ABAKUNDNR = value;
                    NotifyPropertyChanged("ABAKUNDNR");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<ABAUFM> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\ABAUFM\\ABAUFM" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\ABAUFM" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("ABAUFM");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (ABAUFM row in ListInput)
                {
                    XElement f = new XElement("ABAUFM");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement ABAMANDANT = new XElement("ABAMANDANT");
                         ABAMANDANT.Value = row.ABAMANDANT;
                          f.Add(ABAMANDANT);
                        XElement ABAAUFTYP = new XElement("ABAAUFTYP");
                         ABAAUFTYP.Value = row.ABAAUFTYP;
                          f.Add(ABAAUFTYP);
                        XElement ABAAUFKRS = new XElement("ABAAUFKRS");
                         ABAAUFKRS.Value = row.ABAAUFKRS;
                          f.Add(ABAAUFKRS);
                        XElement ABAAUFLFD = new XElement("ABAAUFLFD");
                         ABAAUFLFD.Value = row.ABAAUFLFD;
                          f.Add(ABAAUFLFD);
                        XElement ABAAUFPOS = new XElement("ABAAUFPOS");
                         ABAAUFPOS.Value = row.ABAAUFPOS;
                          f.Add(ABAAUFPOS);
                        XElement ABAAPOS = new XElement("ABAAPOS");
                         ABAAPOS.Value = row.ABAAPOS;
                          f.Add(ABAAPOS);
                        XElement ABAVKAUFTYP = new XElement("ABAVKAUFTYP");
                         ABAVKAUFTYP.Value = row.ABAVKAUFTYP;
                          f.Add(ABAVKAUFTYP);
                        XElement ABAVKAUFKRS = new XElement("ABAVKAUFKRS");
                         ABAVKAUFKRS.Value = row.ABAVKAUFKRS;
                          f.Add(ABAVKAUFKRS);
                        XElement ABAVKAUFLFD = new XElement("ABAVKAUFLFD");
                         ABAVKAUFLFD.Value = row.ABAVKAUFLFD;
                          f.Add(ABAVKAUFLFD);
                        XElement ABAVKAUFPOS = new XElement("ABAVKAUFPOS");
                         ABAVKAUFPOS.Value = row.ABAVKAUFPOS;
                          f.Add(ABAVKAUFPOS);
                        XElement ABAVKUPOS = new XElement("ABAVKUPOS");
                         ABAVKUPOS.Value = row.ABAVKUPOS;
                          f.Add(ABAVKUPOS);
                        XElement ABAKUNDNR = new XElement("ABAKUNDNR");
                         ABAKUNDNR.Value = row.ABAKUNDNR;
                          f.Add(ABAKUNDNR);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
